package org.cgc.gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.cgc.log.GraphLogic;

public class ForbiddenDistancesWindow extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	//GUI
	JLabel lInfo, lForbDist;
	JTextField tfForbDist;
	JButton bOk;
	
	public ForbiddenDistancesWindow() {
		setTitle("Forbidden distances");
		initView();
		pack();
		setVisible(true);
	}
	
	private void initView() {
		String info =
			"<html>" +
			"Define forbidden distances for graph coloring.<br>" +
			"Type values separated by comma (i.e. 0,1,3,5).<br><br>" +
			"Actual forbidden distances:  " +
			GraphLogic.getForbiddenDistances() + "<br>" +
			"</html>";
		
		lInfo = new JLabel(info);
		lForbDist = new JLabel("Forbidden distances:");
		tfForbDist = new JTextField();
		bOk = new JButton("OK");
		bOk.addActionListener(new OKListener());
		JPanel pane = new JPanel(new GridLayout(4, 1));
		pane.add(lInfo);
		pane.add(lForbDist);
		pane.add(tfForbDist);
		pane.add(bOk);
		
		add(pane);
	}
	
	private class OKListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(GraphLogic.initForbiddenDistances(tfForbDist.getText())) {
				ForbiddenDistancesWindow.this.setVisible(false);
			} else {
				JOptionPane.showMessageDialog(ForbiddenDistancesWindow.this,
					    "Cannot initialize variable.",
					    "Number format error",
					    JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
